package com.dap.system.office.controller;

import com.dap.common.core.utils.ServletUtils;
import com.dap.common.core.utils.poi.ExcelUtil;
import com.dap.common.core.web.controller.BaseController;
import com.dap.common.core.web.domain.AjaxResult;
import com.dap.common.core.web.domain.BaseEntity;
import com.dap.common.core.web.page.TableDataInfo;
import com.dap.common.log.annotation.Log;
import com.dap.common.log.enums.BusinessType;
import com.dap.common.security.annotation.PreAuthorize;
import com.dap.common.security.service.TokenService;
import com.dap.system.api.model.LoginUser;
import com.dap.system.office.domain.TTeacher;
import com.dap.system.office.domain.vo.TTeacherSelectorVo;
import com.dap.system.office.service.ITTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 导师Controller
 *
 * @author dap
 * @date 2021-11-10
 */
@RestController
@RequestMapping("/office/teacher")
public class TTeacherController extends BaseController {
    @Autowired
    private ITTeacherService tTeacherService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询导师列表
     */
    @PreAuthorize(hasPermi = "office:teacher:list")
    @GetMapping("/list")
    public TableDataInfo list(TTeacher tTeacher) {
        List<TTeacher> list = tTeacherService.selectTTeacherListPage(tTeacher);
        return getDataTable(list);
    }

    /**
     * 查询导师姓名列表
     */
    @PreAuthorize(hasPermi = "office:teacher:list")
    @GetMapping("/allName")
    public TableDataInfo allName(BaseEntity baseEntity) {
        List<TTeacherSelectorVo> list = tTeacherService.selectTTeacherNameListPage(baseEntity);
        return getDataTable(list);
    }


    /**
     * 导出导师列表
     */
    @PreAuthorize(hasPermi = "office:teacher:export")
    @Log(title = "导师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTeacher tTeacher) throws IOException {
        List<TTeacher> list = tTeacherService.selectTTeacherList(tTeacher);
        for (TTeacher teachers : list) {
            if ("1".equals(teachers.getTeaSex())) {
                teachers.setTeaSex("男");
            } else {
                teachers.setTeaSex("女");
            }
        }
        ExcelUtil<TTeacher> util = new ExcelUtil<TTeacher>(TTeacher.class);
        util.exportExcel(response, list, "teacher");
    }

    /**
     * 导入教师列表
     */
    @PreAuthorize(hasPermi = "office:teacher:import")
    @Log(title = "教师列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TTeacher> util = new ExcelUtil<TTeacher>(TTeacher.class);
        List<TTeacher> teacherList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        Map<String, Object> stringObjectMap = tTeacherService.importTeacher(teacherList, updateSupport, operName);
        return AjaxResult.success(stringObjectMap);
    }

    // 下载模板方法
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<TTeacher> util = new ExcelUtil<TTeacher>(TTeacher.class);
        util.importTemplateExcel(response, "教师列表");
    }

    /**
     * 获取导师详细信息
     */
    @PreAuthorize(hasPermi = "office:teacher:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tTeacherService.selectTTeacherById(id));
    }

    /**
     * 新增导师
     */
    @PreAuthorize(hasPermi = "office:teacher:add")
    @Log(title = "导师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTeacher tTeacher) {
        return toAjax(tTeacherService.insertTTeacher(tTeacher));
    }

    /**
     * 修改导师
     */
    @PreAuthorize(hasPermi = "office:teacher:edit")
    @Log(title = "导师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTeacher tTeacher) {
        return toAjax(tTeacherService.updateTTeacher(tTeacher));
    }

    /**
     * 删除导师
     */
    @PreAuthorize(hasPermi = "office:teacher:remove")
    @Log(title = "导师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTeacherService.deleteTTeacherByIds(ids));
    }
}
